package com.canbot.u05.activity.customvoice.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.bean.CustomDialogExcelDatas
import com.canbot.u05.activity.loge
import kotlinx.android.synthetic.main.custom_dialog_main_item.view.*

/**
 * CustomDialogMainAdapter
 *
 * @author  Liu.Heng
 * @date  2019/5/29
 */
class CustomDialogMainAdapter(var mContext: Context, mQuestionBeanList: MutableList<CustomDialogExcelDatas>, var call: Callback) : RecyclerView.Adapter<CustomDialogMainAdapter.MyViewHolder>() {

    private val layoutInflater: LayoutInflater = LayoutInflater.from(mContext)

    private val mCallback = call

    private var isEdit: Boolean = false

    private var list: MutableList<CustomDialogExcelDatas> = mQuestionBeanList

    interface Callback {

        fun clickEvent(v: View, type: Int)
    }

    private val hashMap = object : HashMap<Int, Boolean>() {
        override fun get(key: Int): Boolean {
            val aBoolean = super.get(key)
            return aBoolean ?: false

        }
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
        val inflate = layoutInflater.inflate(R.layout.custom_dialog_main_item, p0, false)
        return MyViewHolder(inflate)
    }

    override fun getItemCount(): Int {
        loge("CustomDialogMainAdapter, item个数:  ${list.size}")
        return list.size
    }

    fun notify(mList: MutableList<CustomDialogExcelDatas>) {
        this.list = mList
        initData(mList.size, false)
        notifyDataSetChanged()
    }

    fun notifyChooseAll(isChooseAll: Boolean) {
        initData(list.size, isChooseAll)
        notifyDataSetChanged()
    }

    fun setEdit(edit: Boolean) {
        this.isEdit = edit
        notifyDataSetChanged()
    }

    fun initData(size: Int, isChooseAll: Boolean) {
        if (size > 0) {
            (0 until size).forEach { i ->
                hashMap[i] = isChooseAll
            }
        }
    }

    override fun onBindViewHolder(myViewHolder: MyViewHolder, position: Int) {
        Log.e("------>", hashMap[position].toString())
        val checkBox = myViewHolder.checkBox
        val imageView = myViewHolder.imageView1
        val textView = myViewHolder.textView
        textView.text = list[position].listQsBean.first().question
        imageView.tag = position
        imageView.setOnClickListener {
            mCallback.clickEvent(it, 3)
        }
        if (isEdit) {
            checkBox.visibility = View.VISIBLE
            checkBox.tag = position
            checkBox.setOnClickListener {
                val checkbox: CheckBox = it as CheckBox
                if (checkbox.isChecked) {
                    /**每个position为唯一索引,进行存和取*/
                    hashMap[position] = true
                    mCallback.clickEvent(it, 1)
                } else {
                    /**每个position为唯一索引,进行存和取*/
                    hashMap[position] = false
                    mCallback.clickEvent(it, 2)
                }
            }
            checkBox.isChecked = hashMap[position]

        } else {
            checkBox.visibility = View.GONE
        }
    }

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var checkBox: CheckBox = view.choose_btn
        var textView: TextView = view.my_textView
        var imageView1: ImageView = view.imageView1
    }
}

